package com.neusoft.his.sb.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.his.sb.entity.CommonResult;

//这个注解应用于类上，表示这个类中的方法应用于全局所有controller，这个注解有一个value属性，默认
//作用于所有controller
//如果不想作用于所有controller，可以在value属性上指定哪些包可以用
@ControllerAdvice("com.neusoft.his.sb.controller")
//@Controller ，断言，连接点
public class GlobalExceptionResolver {
	
	@ExceptionHandler(HisInputDataExceptuion.class)
	@ResponseBody
	public CommonResult hisInputDataExceptuion(Exception e) {
		System.out.println("GlobalExceptionResolver.hisInputDataExceptuion():" + e.getMessage());
		CommonResult result = new CommonResult();
		result.setCode(500);
		result.setMsg( e.getMessage() );
		result.setData(null);
		return result;
	}
	
	
//	@ExceptionHandler ：  只捕获指定类型的异常对象,其功能类似于 catch块
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody
	public CommonResult hisRuntimeException(Exception e) {
		System.out.println("GlobalExceptionResolver.hisRuntimeException():" + e.getMessage());
		CommonResult result = new CommonResult();
		result.setCode(500);
		result.setMsg("INTERNAL SERVER ERROR 内部错误" );
		result.setData(null);
		return result;
	}

	@ExceptionHandler(SQLException.class)
	@ResponseBody
	public CommonResult hisSQLException(Exception e) {
		System.out.println("GlobalExceptionResolver.hisIOException():" + e.getMessage());
		CommonResult result = new CommonResult();
		result.setCode(500);
		result.setMsg(e.getMessage() );
		result.setData(null);
		return result;
	}

	/**
	 * 拦截所有的异常
	 * 
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public CommonResult<String> exceptionHandler(Exception e) {
		com.mysql.cj.jdbc.exceptions.MysqlDataTruncation aaa;
		// 方法名随便，不过为了望文生意，一般就取为exceptionHandler
		System.out.println("进入自定义异常处理");
		CommonResult result = new CommonResult();
		result.setCode(500);
		result.setMsg("INTERNAL SERVER ERROR 内部错误" );
		result.setData(null);
		return result;
	}
}
